--- title: Blurry keywords: fastai sidebar: home_sidebar summary: "**Blurry** detects faces in photos and blurs them to increase privacy." description: "**Blurry** detects faces in photos and blurs them to increase privacy." nb_path: "00_core.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

Helper function to show cv2 images in Jupyter notebook

{% raw %}
def show_inline_img(img: np.ndarray) -> None:
    display(Image.fromarray(img))
{% endraw %}

Simple Gaussian blur

{% raw %}

blur[source]

blur(img:ndarray, kernel_size=5, sigma_x=0)

{% endraw %} {% raw %}
{% endraw %}

Testing blur:

{% raw %}
test_img = cv2.imread('test_images/group.jpg') #np.zeros((20,20,3), dtype = np.uint8)
assert test_img.shape == blur(test_img).shape, "function should not change shape of array"
assert test_img.shape == blur(test_img, kernel_size=4).shape, "even number for kernel size should be autocorrected"
{% endraw %}

Find faces. Using pretrained haar cascade from OpenCV.

{% raw %}

find_faces[source]

find_faces(img)

Finds faces in a picture and returns tuples of (x, y, w, h) for each face

{% endraw %} {% raw %}
{% endraw %} {% raw %}
faces = find_faces(test_img)
assert len(faces) == 7, "there should be 7 faces in the group test image"
{% endraw %}

Does it work?

{% raw %}
img = test_img.copy()
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
show_inline_img(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
{% endraw %} {% raw %}

blur_areas[source]

blur_areas(img, areas, factor=1)

Blurs defined areas in a cv2 image.

Inputs: img: cv2 image in BGR format areas: tuples of (x, y, w, h) factor: increase (>1.0) or decrease (<1.0) default blurring

Returns: cv2 image in BGR format

{% endraw %} {% raw %}
{% endraw %} {% raw %}

anonymize[source]

anonymize(img, factor=1, convert2rgb=True)

{% endraw %} {% raw %}
{% endraw %} {% raw %}

load_img[source]

load_img(fn)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
img = load_img('test_images/group_closer.jpg')
show_inline_img(anonymize(img, factor=0.5))
{% endraw %} {% raw %}
show_inline_img(anonymize(img, factor=3))
{% endraw %} {% raw %}

test_bulk[source]

test_bulk(directory, **kwargs)

{% endraw %} {% raw %}
{% endraw %} {% raw %}
test_bulk('test_images', factor=1)
{% endraw %}